// import { loginByUsername, logout, getUserInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    Siteflag: '', // 站点 点击上传 分类
    status: '',
    code: '',
    token: getToken('Admin-Token'),
    name: '',
    avatar: '',
    introduction: '',
    roles: [],
    setting: {
      articlePlatform: []
    },
    enterprise: {}, // 企业信息
    superadmin: {}
  },

  mutations: {
    SET_SITEFLAG: (state, val) => { // 站点
      state.Siteflag = val
    },
    SET_superadmin: (state, superadmin) => {
      state.superadmin.data = {
        roles: [superadmin],
        token: superadmin,
        introduction: '我是超级管理员',
        avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
        name: 'Super Admin'
      }
    },
    SET_enterprise: (state, enterprise) => {
      state.enterprise = enterprise
    },
    SET_USER: (state, user) => {
      state.superuser = user
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    }
  },

  actions: {
    SetSiteFlag({ commit }, val) { // 添加站点内容管理的状态
      return new Promise((resolve, reject) => {
        if (val) {
          commit('SET_SITEFLAG', val)
          commit('SET_enterprise', {})
          resolve('ok')
        } else {
          reject('sbm')
        }
      })
    },
    SetEnterprise({ commit }, val) { // 查看企业详情
      return new Promise((resolve, reject) => {
        if (val) {
          commit('SET_enterprise', val)
          commit('SET_SITEFLAG', '')
          resolve('ok')
        } else {
          reject('sbm')
        }
      })
    },
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      // console.log(userInfo);
      const username = userInfo.name.trim()
      return new Promise((resolve, reject) => {
        commit('SET_TOKEN', userInfo.refresh_token)
        commit('SET_USER', userInfo)
        setToken('Admin-Token', userInfo.refresh_token)
        setToken('usertype', userInfo.type)
        setToken('UserInfo', userInfo)
        resolve(userInfo)
      })
    },
    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        // console.log(getToken('usertype'));
        let response = {}
        if (getToken('usertype') == '5') {
          response = {
            data: {
              roles: ['admin'],
              token: 'admin',
              introduction: '我是超级管理员2',
              avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
              name: 'Super Admin'
            }
          }
        } else {
          response = {
            data: {
              roles: ['enterprise'],
              token: 'enterprise',
              introduction: '我是超级管理员2',
              avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
              name: 'Super Admin'
            }
          }
        }

        const data = response.data
        if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
          commit('SET_ROLES', data.roles)
        } else {
          reject('getInfo: roles must be a non-null array!')
        }
        commit('SET_NAME', data.name)
        commit('SET_AVATAR', data.avatar)
        commit('SET_INTRODUCTION', data.introduction)
        resolve(response)
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },

    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken('Admin-Token')
        resolve()
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken('Admin-Token')
        resolve()
      })
    },

    // 动态修改权限
    ChangeRoles({ commit, dispatch }, role) {
      return new Promise(resolve => {
        commit('SET_TOKEN', role)
        setToken('Admin-Token', role)
        console.log(role)
        getUserInfo(role).then(response => {
          const data = response.data
          commit('SET_ROLES', data.roles)
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          dispatch('GenerateRoutes', data) // 动态修改权限后 重绘侧边菜单
          resolve()
        })
      })
    }
  }
}

export default user
